package com.zhu.esyblog.service.impl;

import com.zhu.esyblog.Utils.BeanCopyUtils;
import com.zhu.esyblog.mapper.ArticleMapper;
import com.zhu.esyblog.pojo.Article;
import com.zhu.esyblog.pojo.ArticleBody;
import com.zhu.esyblog.mapper.ArticleBodyMapper;
import com.zhu.esyblog.pojo.RespBean;
import com.zhu.esyblog.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.esyblog.vo.ArticleBodyVo;
import com.zhu.esyblog.vo.ArticleVo;
import com.zhu.esyblog.vo.CategoryVo;
import com.zhu.esyblog.vo.TagVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stx
 * @since 2022-06-22
 */
@Service
public class ArticleBodyServiceImpl extends ServiceImpl<ArticleBodyMapper, ArticleBody> implements IArticleBodyService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleBodyMapper articleBodyMapper;
    @Autowired
    private ITagService tagService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private ThreadService threadService;
    /**
     * 查看文章详情
     * @param articleId
     * @return
     */
    @Override
    public RespBean findArticleById(Long articleId) {
        /**
         * 1.根据id查询文章信息
         * 2.根据body id和分类id关联查询
         * important 3.当查看文章之后本应将阅读次数增加一次，
         * 这是更新操作，但是更新操作的时候数据库是枷锁的，会堵塞其他操作，性能会比较低
         * 一旦更新出问题，不能影响文章查询的问题
         * 引入线程池  可以把更新操作扔到线程池中进行操作，和主线程就不相关了
         */
        Article article = articleMapper.selectById(articleId);
        ArticleVo articleVo = copy(article, true, true, true, true);
        threadService.updateArticleVoCount(articleMapper,article);
        return RespBean.success("success",articleVo);
    }


    private List<ArticleVo> copyList(List<Article> records, Boolean isTag, Boolean isAuthor) {
        List<ArticleVo> articleVoList=new ArrayList<>();
        for (Article record : records) {
            articleVoList.add(copy(record,isTag,isAuthor,false,false));
        }
        return articleVoList;
    }
    private List<ArticleVo> copyList(List<Article> records, Boolean isTag, Boolean isAuthor,boolean isBody,boolean isCategory) {
        List<ArticleVo> articleVoList=new ArrayList<>();
        for (Article record : records) {
            articleVoList.add(copy(record,isTag,isAuthor,isBody,isCategory));
        }
        return articleVoList;
    }

    private ArticleVo copy(Article article,Boolean isTag,Boolean isAuthor,boolean isBody,boolean isCategory){
        ArticleVo articleVo=new ArticleVo();
        BeanUtils.copyProperties(article,articleVo);
        articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        if(isTag){
            Long articleId = article.getId();
            List<TagVo> tagsByArticleId = tagService.findTagsByArticleId(articleId);
            for (TagVo tagVo : tagsByArticleId) {
                System.out.println("=========================");
                System.out.println(tagVo);
            }
            articleVo.setTags(tagService.findTagsByArticleId(articleId));
        }
        if(isAuthor){
            Long authorId = article.getAuthorId();
            articleVo.setAuthor(userService.findUserById(authorId).getNickname());
        }
        if(isBody){
            Long bodyId=article.getBodyId();
            articleVo.setBody(findArticleBodyById(bodyId));
        }
        if(isCategory){
            Integer categoryId = article.getCategoryId();
            CategoryVo categoryVo = categoryService.findCateGoryById(categoryId);
            articleVo.setCategory(categoryVo);

        }
        return  articleVo;
    }

    private ArticleBodyVo findArticleBodyById(Long bodyId) {
        ArticleBody articleBody = articleBodyMapper.selectById(bodyId);
        ArticleBodyVo articleBodyVo = BeanCopyUtils.copyBean(articleBody, ArticleBodyVo.class);
        return articleBodyVo;
    }
}
